#include "MJStone.h"
#include<QMessageBox>
#include<QMessageBox>
map<int,int>  MJStone::allStone   ;
vector<int>   MJStone::allKindStone;
vector<int>   MJStone::handStoneVec ;
vector<int>   MJStone::waitStoneVec;

int           MJStone::disCard;
//map<int,int>  MJStone::handStoneMap;

void MJStone::init(){
    allKindStone.push_back(Wan1 );
    allKindStone.push_back(Wan2 );
    allKindStone.push_back(Wan3 );
    allKindStone.push_back(Wan4 );
    allKindStone.push_back(Wan5 );
    allKindStone.push_back(Wan6 );
    allKindStone.push_back(Wan7 );
    allKindStone.push_back(Wan8 );
    allKindStone.push_back(Wan9 );
    allKindStone.push_back(Tiao1);
    allKindStone.push_back(Tiao2);
    allKindStone.push_back(Tiao3);
    allKindStone.push_back(Tiao4);
    allKindStone.push_back(Tiao5);
    allKindStone.push_back(Tiao6);
    allKindStone.push_back(Tiao7);
    allKindStone.push_back(Tiao8);
    allKindStone.push_back(Tiao9);
    allKindStone.push_back(Tong1);
    allKindStone.push_back(Tong2);
    allKindStone.push_back(Tong3);
    allKindStone.push_back(Tong4);
    allKindStone.push_back(Tong5);
    allKindStone.push_back(Tong6);
    allKindStone.push_back(Tong7);
    allKindStone.push_back(Tong8);
    allKindStone.push_back(Tong9);
    allKindStone.push_back(Dong );
    allKindStone.push_back(Nan  );
    allKindStone.push_back(Xi   );
    allKindStone.push_back(Bei  );
    allKindStone.push_back(Bai  );
    allKindStone.push_back(Zhong);
    allKindStone.push_back(Fa   );
    for(auto a:allKindStone){
        allStone[a] = 4;
    }
}

QString MJStone::stoneToName(int num){
    map<int,QString> Map;
    Map[  1] = "一万";
    Map[  2] = "二万";
    Map[  3] = "三万";
    Map[  4] = "四万";
    Map[  5] = "五万";
    Map[  6] = "六万";
    Map[  7] = "七万";
    Map[  8] = "八万";
    Map[  9] = "九万";
    Map[ 11] = "一条";
    Map[ 12] = "二条";
    Map[ 13] = "三条";
    Map[ 14] = "四条";
    Map[ 15] = "五条";
    Map[ 16] = "六条";
    Map[ 17] = "七条";
    Map[ 18] = "八条";
    Map[ 19] = "九条";
    Map[ 21] = "一筒";
    Map[ 22] = "二筒";
    Map[ 23] = "三筒";
    Map[ 24] = "四筒";
    Map[ 25] = "五筒";
    Map[ 26] = "六筒";
    Map[ 27] = "七筒";
    Map[ 28] = "八筒";
    Map[ 29] = "九筒";
    Map[ 40] = "东风";
    Map[ 50] = "南风";
    Map[ 60] = "西风";
    Map[ 70] = "北风";
    Map[ 80] = "白板";
    Map[ 90] = "红中";
    Map[100] = "发财";
    return Map[num];
}

QPixmap MJStone::getPicMapByNum(int i){
    QPixmap img;
    QString file = QString::number(i,10);
    img.load(QString("../res/") + file + QString(".png"));
//    img.load(QString("../res/") + i + QString(".png"));
    return img;
}


//麻将顺子与刻子的回溯删除法
bool MJStone::helper(map<int, int>& Map) {
    if (Map.empty())
        return true;
    bool res = false;
    int a = Map.begin()->first;
    if (Map[a] >= 3) {
        if (!(Map[a] -= 3))
            Map.erase(a);
        res = helper(Map);
        Map[a] += 3;
    }
    if (Map.size() >= 3) {
        if (Map.find(a + 1) != Map.end() &&
            Map.find(a + 2) != Map.end()) {
            if (!--Map[a])
                Map.erase(a);
            if (!--Map[a + 1])
                Map.erase(a + 1);
            if (!--Map[a + 2])
                Map.erase(a + 2);
            res |= helper(Map);
            ++Map[a];
            ++Map[a + 1];
            ++Map[a + 2];
        }
    }
    return res;
}
//胡牌算法

bool MJStone::win(vector<int> &vec) {
    if (vec.empty())
        return true;
    map<int, int> Map;
    for (auto i : vec){
        Map[i]++;
    }
    for (auto &p : Map) {
        if (p.second >= 2) {
            map<int, int> tmp(Map);
            if (!(tmp[p.first] -= 2))
                tmp.erase(p.first);
            if (helper(tmp))
                return true;
        }
    }
    return false;
}

bool MJStone::wait(vector<int> &vec){
    for (auto i : allKindStone){
        vec.push_back(i);
        if (win(vec)){
            if(allStone[i] > 0){
                waitStoneVec.push_back(i);
            }
        }
        vec.pop_back();
    }
    if (!waitStoneVec.empty()){
        cout<< "听牌：";
        for (auto i : waitStoneVec){
            cout << i << " ";
        }
        return true;
    }
    return false;
}

QString intToStr(int i){
    QString str = QString::number(i,10);
    return str;
}

bool MJStone::discard(vector<int> &vec){
    int i = vec.size();
    int temp = 0;
    while (i--){
        temp = vec[0];
        vec.erase(vec.begin());
        if (wait(vec)){
            vec.push_back(temp);
            disCard = temp;
            return true;
        }
        vec.push_back(temp);
    }
    return false;
}
